package com.cc.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.collection.CollectionUtil;
import com.cc.base.PageResponse;
import com.cc.base.ResponseResult;
import com.cc.pojo.domain.Role;
import com.cc.pojo.dto.PermissionsDto;
import com.cc.pojo.dto.RplePageDto;
import com.cc.pojo.vo.OptionsVo;
import com.cc.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/role/")
@Slf4j
@Tag(name = "角色管理")
public class RoleController {
    @Resource
    private RoleService roleService;

    /**
     * 添加角色
     */
    @PostMapping()
    @Operation(summary = "添加角色")
    @SaCheckPermission("RoleList")
    public ResponseResult<String> addRole(@RequestBody Role role) {
        return roleService.addRole(role);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除角色")
    @SaCheckPermission("RoleList")
    public ResponseResult<Boolean> removeRole(@PathVariable List<Long> ids) {
        //1.检查参数
        if (CollectionUtil.isEmpty(ids)) {
            return ResponseResult.error("参数为空");
        }
        boolean removeById = roleService.removeByIds(ids);
        if (!removeById) {
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.ok(Boolean.TRUE);
    }


    /**
     * 修改角色
     */
    @PutMapping()
    @Operation(summary = "修改角色")
    @SaCheckPermission("RoleList")
    public ResponseResult<String> updateRole(@RequestBody Role role) {
        return roleService.updateRole(role);
    }


    /**
     * 角色分页查询
     */
    @PostMapping("/page")
    @Operation(summary = "角色分页查询")
    @SaCheckPermission("RoleList")
    public ResponseResult<PageResponse<Role>> rolePage(@RequestBody RplePageDto pageDto) {
        return roleService.rolePage(pageDto);
    }

    /**
     * 根据id查询角色信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询角色信息")
    @SaCheckPermission("RoleList")
    public ResponseResult<Role> getById(@PathVariable("id") Long id) {
        //1.检查参数
        if (id == 0 || id == null) {
            return ResponseResult.error("参数为空");
        }
        Role role = roleService.getById(id);
        return ResponseResult.ok(role);
    }

    /**
     * 根据角色id获取权限列表
     *
     * @return
     */
    @GetMapping("/permissions/{roleId}")
    @Operation(summary = "根据角色id获取权限列表id")
    @SaCheckPermission("RoleList")
    public ResponseResult<List<String>> getPermissionsRoleId(@PathVariable Long roleId) {
        return roleService.getPermissionsRoleId(roleId);
    }

    /**
     * 修改角色权限
     *
     * @return
     */
    @PostMapping("/permissions")
    @Operation(summary = "修改角色权限")
    @SaCheckPermission(value = {"RoleList", "UserList", "AdminUserList", "RegularUserList"}, mode = SaMode.OR)
    public ResponseResult<List<String>> updatePermissions(@RequestBody PermissionsDto permissionsDto) {
        return roleService.updatePermissions(permissionsDto);
    }


    /**
     * 获取权限权限列表
     *
     * @return
     */
    @GetMapping("/permissions/list")
    @Operation(summary = "获取权限权限列表")
    @SaCheckPermission(value = {"RoleList", "UserList", "AdminUserList", "RegularUserList"}, mode = SaMode.OR)
    public ResponseResult<List<OptionsVo>> permissionsList() {
        return roleService.permissionsList();
    }

}
